package cn.botter.subject.application.controller;

import cn.botter.subject.application.convert.SubjectAnswerListDTOConverter;
import cn.botter.subject.application.convert.SubjectInfoDTOConverter;
import cn.botter.subject.application.dto.SubjectAnswerListDTO;
import cn.botter.subject.application.dto.SubjectInfoDTO;
import cn.botter.subject.common.entity.PageResult;
import cn.botter.subject.common.entity.Result;
import cn.botter.subject.common.enums.IsDeletedEnum;
import cn.botter.subject.domain.entity.SubjectAnswerBO;
import cn.botter.subject.domain.entity.SubjectInfoBO;
import cn.botter.subject.domain.service.SubjectDomainInfoService;
import cn.botter.subject.infra.basic.entity.SubjectInfo;
import cn.botter.subject.infra.basic.entity.SubjectInfoES;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectInfoController {
    @Resource
    private SubjectDomainInfoService subjectDomainInfoService;

    /**
     * 这是一个添加题目的接口
     *
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.add.dto:{}", JSON.toJSONString(subjectInfoDTO));

            }
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "题目分类id不能为空");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "题目标签id不能为空");
//            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getOptionList()),"题目的答案List不能为空");
            /*
                将传入的形参转换为BO类型，我们再用这个BO去调用infra层的sql
             */

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);

            List<SubjectAnswerListDTO> optionList = subjectInfoDTO.getOptionList(); //将optionList转化为BO

            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerListDTOConverter.INSTANCE.convertDtoToBoList(optionList);
            subjectInfoBO.setOptionList(subjectAnswerBOList);
            subjectInfoBO.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            subjectDomainInfoService.add(subjectInfoBO);
            return Result.OK(true);

        } catch (Exception e) {
            log.info("SubjectLabelController.add.error:{}", e.getMessage(), e);
            return Result.FAILED(e.getMessage());
        }
    }

    /**
     * 这是一个获取题目详细信息的接口
     *
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));

            }
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "题目标签不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "题目分类不能为空");

            /*
                将传入的形参转换为BO类型，我们再用这个BO去调用infra层的sql
             */
//            SubjectInfo test = new SubjectInfo();
//            test.get
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);

            PageResult<SubjectInfoBO> subjectPage = subjectDomainInfoService.getSubjectPage(subjectInfoBO);
            return Result.OK(subjectPage);

        } catch (Exception e) {
            log.info("SubjectLabelController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.FAILED(e.getMessage());
        }
    }

    /**
     * 这是一个根据题目id进行查询详细信息
     *
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfo> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDTO));

            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");


            /*
                将传入的形参转换为BO类型，我们再用这个BO去调用infra层的sql
             */

            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            SubjectInfoBO resultInfoBO = subjectDomainInfoService.querySubjectInfo(subjectInfoBO);

            SubjectInfoDTO res = SubjectInfoDTOConverter.INSTANCE.convertBoToDto(resultInfoBO);
//            PageResult<SubjectInfoBO> subjectPage = subjectDomainInfoService.getSubjectPage(subjectInfoBO);
            return Result.OK(res);

        } catch (Exception e) {
            log.info("SubjectLabelController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.FAILED(e.getMessage());
        }
    }

    /**
     * 这是一个全局es的搜索接口
     *
     * @param subjectInfoDTO
     * @return
     */
    @RequestMapping("/getSubjectPageBySearch")
    public Result<List<SubjectInfoES>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getKeyWord(), "关键词不能为空");

            /*
                将传入的形参转换为BO类型，我们再用这个BO去调用infra层的sql
             */
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            PageResult<SubjectInfoES> subjectPage = subjectDomainInfoService.getSubjectPageBySearch(subjectInfoBO);
            return Result.OK(subjectPage);
        } catch (Exception e) {
            log.info("SubjectLabelController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.FAILED(e.getMessage());
        }
    }

    /**
     * 用户排行榜功能（根据用户出题的数目进行动态排行处理
     */

    @RequestMapping("/getContributeList")
    public Result<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> boList = subjectDomainInfoService.getContributeList();
            List<SubjectInfoDTO> subjectInfoDTOS = SubjectInfoDTOConverter.INSTANCE.convertBoToDtoList(boList);
            return Result.OK(subjectInfoDTOS);
        } catch (Exception e) {
            log.info("SubjectLabelController.getContributeList.error:{}", e.getMessage(), e);
            return Result.FAILED("返回文章贡献榜单失败！");
        }
    }
}
